package net.openvision.music.ui;

import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.Scrollable;
import javax.swing.UIManager;

import net.openvision.music.model.Bar;
import net.openvision.music.model.DefaultScoreModel;
import net.openvision.music.model.ScoreDataEvent;
import net.openvision.music.model.ScoreDataListener;
import net.openvision.music.model.ScoreModel;

import static net.openvision.music.model.ScoreDataEvent.SCORE_OBJECT_ADDED;
import static net.openvision.music.model.ScoreDataEvent.SCORE_OBJECT_REMOVED;
import static net.openvision.music.model.ScoreDataEvent.SCORE_OBJECT_CHANGED;

public class JMusicScore extends JComponent implements ScoreDataListener, Scrollable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String uiClassID = "MusicScoreUI";

	private List<JBar[]> barUI;
	
	protected ScoreModel model;
	protected BarRenderer renderer;
	
	private void addBars(Bar[] bars) {
		JBar[] uiBars = new JBar[model.getStaffCount()];
		for (int i = 0; i < uiBars.length; i++) {
			uiBars[i] = new JBar(bars[i]);
			add(uiBars[i]);
		}
		barUI.add(uiBars);
	}
	
	private void clear() {
		barUI.clear();
		removeAll();
	}

	protected void sizeChanged() {
		//setPreferredSize(layout.computeSize(model));
		revalidate();
	}

	public JMusicScore(ScoreModel model, LayoutManager layout) {
		updateUI();
		this.model = model;
		this.barUI = new ArrayList<JBar[]>();
		setLayout(layout);
		
		model.addListener(this);
		
	}

	public JMusicScore(LayoutManager layout) {
		this(new DefaultScoreModel(), layout);
	}
	
	public JMusicScore() {
		this(new LineLayout());
	}
	
	public JBar[] getBarsAt(int index) {
		return barUI.get(index);
	}

	public ScoreModel getModel() {
		return model;
	}

	public void setModel(ScoreModel model) {
		this.model = model;
		model.removeListener(this);
		model.addListener(this);
		
		clear();
		for (int i = 0; i < model.getBarCount(); i++) {
			addBars(model.getBarsAt(i));
		}
		
		getLayout().layoutContainer(this);
	}

//	public void setUI(MusicScoreUI ui) {
//		super.setUI(ui);
//	}
//
//	public void updateUI() {
//		if (UIManager.get(getUIClassID()) != null) {
//			setUI((MusicScoreUI) UIManager.getUI(this));
//		} else {
//			setUI(new BasicMusicScoreUI());
//		}
//	}
//
//	public MusicScoreUI getUI() {
//		return (MusicScoreUI) ui;
//	}
//
//	public String getUIClassID() {
//		return uiClassID;
//	}
	
	@Override
	public Dimension getPreferredSize() {
		return getLayout().preferredLayoutSize(this);
	}

	@Override
	public Dimension getPreferredScrollableViewportSize() {
		return getPreferredSize();
	}

	@Override
	public int getScrollableBlockIncrement(Rectangle visibleRect,
			int orientation, int direction) {
		return 0;
	}

	@Override
	public boolean getScrollableTracksViewportHeight() {
		return false;
	}

	@Override
	public boolean getScrollableTracksViewportWidth() {
		return false;
	}

	@Override
	public int getScrollableUnitIncrement(Rectangle visibleRect,
			int orientation, int direction) {
		return 0;
	}

	@Override
	public void documentInformationChanged(String name, String newValue) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void scoreDataChanged(ScoreDataEvent event) {
		switch (event.getEventType()) {
		case SCORE_OBJECT_ADDED:
			if (event.getObject() instanceof Bar[]) {
				addBars((Bar[])event.getObject());
			}
			break;
		case SCORE_OBJECT_REMOVED:
			break;
		case SCORE_OBJECT_CHANGED:
			break;
		}
	}
}
